package com.ym.b2b.payment.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.payment.dao.EmSPaymentDAO;
import com.ym.b2b.payment.service.EmSPaymentService;
import com.ym.b2b.payment.sqlbean.EmSPayment;
import com.ym.b2b.payment.vo.PaymentBean;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/**
 * @ClassName: EmSPaymentServiceImpl
 * @Author tangxj
 * @Date 2015-3-18 上午10:55:57
 * @Modify
 * @CopyRight B2B小组
 */

@Service
public class EmSPaymentServiceImpl extends BaseService implements EmSPaymentService {

    private Logger        logger = Logger.getLogger(getClass());
    @Autowired
    private EmSPaymentDAO emSPaymentDAO;
    @Autowired
    private EmCUserDAO    userdao;

    @Override
    public Result doPayBefore(PaymentBean s) throws BaseException {
        Result ret = new Result();
        try {
            // 业务check

            EmSPayment emSPayment = new EmSPayment();
            emSPayment.setOrderCode(s.getOrderCode());
            // 检查是否已存在支付信息
            EmSPayment emSPayment1 = emSPaymentDAO.searchEmSPayment(emSPayment);
            if (emSPayment1 != null) {
            	emSPayment.setPayerType(s.getPayerType());
            	emSPayment.setPaymentMoney(s.getPaymentMoney());
            	emSPayment.setPaymentRequestIp(s.getPaymentRequestIp());
                emSPaymentDAO.updateEmSPayment(emSPayment);
                emSPayment1.setPayerType(s.getPayerType());
                emSPayment1.setPaymentMoney(s.getPaymentMoney());
                emSPayment1.setPaymentRequestIp(s.getPaymentRequestIp());
                ret.putData("emSPayment", emSPayment1);
            } else {
                emSPaymentDAO.insertEmSPayment(emSPayment);
                ret.putData("emSPayment", emSPayment);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, "支付请求提交失败!");
        }
        return ret;
    }

    @Override
    public Result searchPageEmSPayment(Page<EmSPayment> page, EmSPayment emSPayment) throws DataAccessException {
        Result result = new Result();

        try {

            result.setResultPage(emSPaymentDAO.searchPageEmSPayment(emSPayment, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    @Override
    public Result insertEmSPayment(EmSPayment emSPayment) throws DataAccessException {
        Result result = checkInputEmSPayment(emSPayment);
        if (!result.getCode().equals("200")) return result;
        try {
            emSPaymentDAO.insertEmSPayment(emSPayment);
            result.setResult("添加支付成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "添加支付失败！");

        }
        return result;
    }

    @Override
    public Result updateEmSPayment(EmSPayment emSPayment) throws DataAccessException {
        Result result = checkInputEmSPayment(emSPayment);
        if (!result.getCode().equals("200")) return result;
        try {
            emSPaymentDAO.updateEmSPayment(emSPayment);
            result.setResult("修改支付成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "修改支付失败！");

        }
        return result;
    }

    /**
     * 输入参数检查
     * 
     * @Title: checkInputEmSPayment
     */
    private Result checkInputEmSPayment(EmSPayment p) {
        Result result = new Result();
        return result;
    }
}
